package com.example.ymm.modules1.service.impl.ms;

import com.baomidou.dynamic.datasource.annotation.Slave;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.ms.VMaterial;
import com.example.ymm.modules1.mapper.ms.VMaterialMapper;
import com.example.ymm.modules1.query.ms.VMaterialQueryCriteria;
import com.example.ymm.modules1.service.ms.VMaterialService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
//@Transactional(propagation = REQUIRES_NEW)
@Slf4j
@Slave
public class VMaterialServiceImpl extends BaseServiceImpl<VMaterialMapper, VMaterial> implements VMaterialService {


    @Override
    public TableList<VMaterial> list(VMaterialQueryCriteria criteria, YmPageDto ymPage) {
//        IPage<VMaterial> page = new Page<VMaterial>(ymPage.getPage(),ymPage.getPageSize());
//        QueryWrapper predicate = QueryHelpPlus.getPredicate(VMaterial.class, criteria);
//        predicate.orderByDesc("oid");
//        baseMapper.selectPage(page, predicate);
        getPage(ymPage);
        List<VMaterial> vMaterials = baseMapper.queryList(criteria);
        PageInfo<VMaterial> pageInfo = new PageInfo<>(vMaterials);
        TableList<VMaterial> tableList=new TableList(pageInfo);
        return tableList;
    }

    @Override
    public List<VMaterial> queryAll(VMaterialQueryCriteria criteria) {
        List<VMaterial> result =baseMapper.selectList(QueryHelpPlus.getPredicate(VMaterial.class, criteria));
        return result;
    }

    @Override
    public List<VMaterial> queryList(VMaterialQueryCriteria criteria) {
        return baseMapper.queryList(criteria);
    }

    @Override
    public VMaterial findByMnameAndGuige(String mname, String guige) {
        VMaterialQueryCriteria criteria=new VMaterialQueryCriteria();
        criteria.setMname(mname);
        criteria.setMmodel(guige);
        List<VMaterial> vMaterials = queryAll(criteria);
        VMaterial result=null;
        if(UtilValidate.isNotEmpty(vMaterials)){
            result=vMaterials.get(0);
        }
        return result;
    }

    @Override
    public VMaterial findByNo(String no) {
        VMaterialQueryCriteria criteria=new VMaterialQueryCriteria();
        criteria.setMtid(no);
        VMaterial result=null;
        List<VMaterial> vMaterials = queryAll(criteria);
        if(UtilValidate.isNotEmpty(vMaterials)){
            result=vMaterials.get(0);
        }
        return result;
    }
}
